(function($) {
	
	$.fn.isCreatePm = function(){
		
		return $(this).each(function(){
			
			var replyForm = $('form'),
			    receiver = $('#receive',this),
			    warnSpan = $('.message-warn',this),
			    writeTextarea = $('#pm_content',this);
			    cancelBtn = $(".cancel",this),
			    contextPath = $("#context_path",this).val();
			    
			$(replyForm).live('submit2',function(){
				if($(receiver).val() == ''){
					$(warnSpan).show().text(messages.recieverNull);
				}
				else if($(writeTextarea).val() == ''){
					$(writeTextarea).css('background','#f5cfcf');
					$(writeTextarea).animate({
					    opacity: 0.3
					  }, 300, function() {
						  $(writeTextarea).css('background','#fff').css('opacity', '1');
					  });
				}
				else{
					$.ajax({
						async:false,
						url:$(this).attr('action'),
						type:'POST',
						dataType:'json',
						data:$(this).serialize(),
						beforeSend: function (xhr) {
							 xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
						},
						success:function(json){
							if(json.success){
								$(warnSpan).hide();
								var position = $('.submit-btn').offset();
								$('#save-pop .save-success').html(messages.sendSuccessfully);
								$('#save-pop').css('position','absolute')
								  			.css('top',position.top - 10)
									  		.css('left',position.left - 170)
									  		.css('background','#fafafa')
									  		.fadeIn().delay(500);
								setTimeout(function(){
									if($(window.parent.document).find('.content').hasClass('refresh')){
										parent.location.reload(); 
									}
									else{
										window.top.$.wokaobox.close();
									}
									
								},1000);
							}
							else{
								$(warnSpan).show().text(json.message);
							}
						}
					});
				}
				return false;
			});
			
			$(cancelBtn).click(function(){
				window.top.$.wokaobox.close();
				return false;
			});
			
//			$.get(contextPath + '/save/popup', function(html) {
//				  $('body').append(html).css('margin-bottom', '5px');
//			});
			
			$(writeTextarea).maxlength({
				statusElement:$('.wordNum'),
				maxlength:300
			});
		});
	};
})(jQuery);